<link href="/css/style.css" rel="stylesheet">
<link href="/css/style-responsive.css" rel="stylesheet">
<link href="/js/advanced-datatable/css/demo_page.css" rel="stylesheet"/>
<link href="/js/advanced-datatable/css/demo_table.css" rel="stylesheet"/>
<link rel="stylesheet" href="/js/data-tables/DT_bootstrap.css"/>

<link href="/css/style.css" rel="stylesheet">
<link href="/css/style-responsive.css" rel="stylesheet">

<body class="panel" style="padding: 0;">
<div class="row">
    <div class="col-sm-12">
        <div class="panel-body">
            <div class="center-panel">
                <div id="signupForm" class="form-horizontal adminex-form">
                    <div class="row">
                        <div class="col-md-2">
                            <div class="form-group">
                                <label>日志类别</label>
                                <select id="log_type" name="log_type" class="form-control">
                                    <option value="0">全部类别</option>
                                    <option value="1">用户日志</option>
                                    <option value="2">app操作日志</option>
                                    <option value="3">app patch 日志</option>
                                    <option value="4">对外api账户</option>
                                    <option value="5">对外api地址</option>
                                    <option value="6">用户权限日志</option>
                                    <option value="7">patch推送日志</option>
                                    <option value="8">event推送日志</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="form-group">
                                <label>操作人昵称</label>
                                <input type="text" class="form-control" id="nick_name" name="nick_name">
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="form-group">
                                <label>&nbsp;</label><br/>
                                <button type="button" class='btn btn-success  btn-sm'
                                        onclick="	 currentPage =1;searchLog()">查询
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="adv-table ">
                <table class="display table table-striped" id="listTable" width="100%">
                    <thead>
                    <tr>
                        <th width="100px" contenteditable="false">编号</th>
                        <th width="20%">日志类别</th>
                        <th width="50%">内容</th>
                        <th width="100px">操作人</th>
                        <th width="20%">发生时间</th>
                    </tr>
                    </thead>

                    <tbody id="maintablebody">
                    </tbody>
                </table>
            </div>
            <footer>
                <label>
                    <select class="form-control" id="stepInput" onchange="currentPage = 1;changeStep(false)" size="1"
                            name="hidden-table-info_length" aria-controls="hidden-table-info">
                        <option value="10" selected="selected">10</option>
                        <option value="25">25</option>
                        <option value="50">50</option>
                        <option value="100">100</option>
                    </select> records per page</label>

                <div id="pageLabelDiv" class="dataTables_paginate paging_bootstrap pagination">
                    <ul>
                    </ul>
                </div>
            </footer>
        </div>

    </div>
</div>

<!-- Placed js at the end of the document so the pages load faster -->
<script src="/js/html5shiv.js"></script>
<script src="/js/respond.min.js"></script>

<script src="/js/jquery-1.10.2.min.js"></script>
<script src="/js/jquery-ui-1.9.2.custom.min.js"></script>
<script src="/js/jquery-migrate-1.2.1.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/modernizr.min.js"></script>
<script src="/js/jquery.nicescroll.js"></script>
<!--common scripts for all pages-->
<script src="/js/scripts.js"></script>

<script>
    var totalCount = 0
    var currentPage = 1
    var step = 10
    var currentId = 0
    var tablebody
    var currentListData
    var adddata
    var host
    //从API获取数据列表
    function getList(page, tempstep) {
        currentPage = page;
        step = tempstep
        searchLog();
    }
    //页面载入后先获取默认条数的数据
    window.onload = function () {
        host = "http://" + window.location.host
        getList(currentPage, step);
    };
    function onBodyResize() {

    }
    //采用本地数组中的数据填充表格
    function fillTable() {
        adddata = "";
        $.each(currentListData, function (index, item) {
            adddata += ("<tr ");
            if (item.enable == 0) {
                adddata += ("  bgcolor='#FFFFdd' ");
            }
            adddata += ("  name=" + index + ">");
            adddata += ("<td>" + item.id + "</td>");
            adddata += ("<td width='200'>" + getTypeByid(item.log_type) + "</td>");
            adddata += ("<td width='600'>" + item.content + "</td>");
            adddata += ("<td>" + item.user_nickname + "</td>");
            adddata += ("<td>" + item.logtime + "</td>");

            adddata += "</tr>";

        });
        tablebody.append(adddata);
    }

    function getTypeByid(typeid) {
        var result = ""
        switch (typeid) {
            case '1':
                result = "用户";
                break;
            case '2':
                result = "App";
                break;
            case '3':
                result = "Patch";
                break;
            case '4':
                result = "对外api user";
                break;
            case '5':
                result = "对外api url";
                break;
            case '6':
                result = "用户授权";
                break;
            case '7':
                result = "patch推送";
                break;
            case '8':
                result = "Event推送";
                break;
        }
        return result
    }

    //清空数据表格
    function clearDataTable() {
        tablebody = $("#maintablebody");
        tablebody.empty();
    }
    //从数据库中获取列表成功
    function listToSuccess(response, status, xhr) {
        if (status == "success" && response.reusltCode == "200") {
            currentListData = response.reusltDetail;
            totalCount = response.totalCount;
            clearDataTable();
            changeStep(true);
            fillTable();
            parent.setMainContentHeight(currentListData.length, 52);
        } else {
            alert(response);
        }

    }
    //点击提交按钮
    function searchLog() {
        var currentdata = new Object();
        currentdata.nick_name = document.getElementById("nick_name").value;
        currentdata.log_type = document.getElementById("log_type").value;

        $.post(host + "/list_log/" + currentPage + "/step/" + step, JSON.stringify(currentdata), listToSuccess, "json");
    }

    //每页的条数发生改变时  重新计算分页条数
    function changeStep(onlyChangeStep) {
        var pagelabelDiv = $("#pageLabelDiv ul");
        pagelabelDiv.empty();
        var stepinput = document.getElementById("stepInput")
        step = parseInt(stepinput.options[stepinput.selectedIndex].value);
        var pageNum = Math.ceil(totalCount / step);
        if (currentPage <= 1) {
            pagelabelDiv.append('<li class="prev disabled"><a href="#">← Previous</a></li>')
        } else {
            pagelabelDiv.append('<li class="prev" onclick="getList(' + (currentPage - 1) + ',' + step + ')" ><a href="#">← Previous</a></li>')
        }
        if (pageNum > 6) {
            for (var i = 1; i <= pageNum; i++) {
                if ((i) == currentPage) {
                    pagelabelDiv.append('<li class="active"><a   >' + (i) + '</a></li>')
                } else if ((i <= 2)) {
                    pagelabelDiv.append('<li onclick="getList(' + i + ',' + step + ')"><a  href="#">' + (i) + '</a></li>')
                } else if ((i >= currentPage - 1 && (i <= currentPage + 1)) || (i > pageNum - 2)) {
                    pagelabelDiv.append('<li onclick="getList(' + i + ',' + step + ')"><a  href="#">' + (i) + '</a></li>')
                }
            }

        } else {
            for (var i = 1; i <= pageNum; i++) {
                if ((i) == currentPage) {
                    pagelabelDiv.append('<li class="active"><a   >' + (i) + '</a></li>')
                } else {
                    pagelabelDiv.append('<li onclick="getList(' + i + ',' + step + ')"><a  href="#">' + (i) + '</a></li>')
                }
            }
        }
        if (currentPage >= pageNum) {
            pagelabelDiv.append('<li class="next disabled"><a href="#">Next → </a></li>')
        } else {
            pagelabelDiv.append('<li class="next" onclick="getList(' + (currentPage + 1) + ',' + step + ')" ><a href="#">Next → </a></li>')
        }
        if (onlyChangeStep == false) {
            getList(currentPage, step);
        }
    }
</script>
</body>